package com.paul.sys.controller.onpublic;


import com.paul.sys.model.Ptstaff;
import com.paul.sys.model.Symenu;
import com.paul.sys.service.SymenuService;
import com.paul.sys.util.CommSession;
import com.paul.sys.util.MapUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/onpublic/")
public class PublicIndexController {
    private Logger log = Logger.getLogger(PublicIndexController.class);
    @Autowired
    private SymenuService symenuService;
    @ResponseBody
    @RequestMapping("index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView view = new ModelAndView("public/index");

        view.addObject("user",CommSession.getUser(request));
        return view;
    }
    @RequestMapping("welcome")
    public String welcome(){
        return "back/welcome";
    }

    /**
     * 根据用户名获取所有菜单
     * @return
     */
    @ResponseBody
    @RequestMapping("getMenu")
    public Collection<Symenu> getMenu(HttpServletRequest request){
        Map<String,String> param = new HashMap<String,String>();
        Ptstaff staff = CommSession.getUser(request);
        if(staff!=null){

            param.put("unid",staff.getUnid()+"");

        }else{
            param.put("unid","100001");
        }
        param.put("menulevel","1");
        param.put("punid","0");
     //  List<Symenu> list =  symenuService.listPrivateByUserId(param);
//        List<String> menus = new ArrayList<>();
//        for(Symenu menu:list){
//            param.put("menulevel","2");
//            param.put("punid",menu.getUnid()+"");
//            List<Symenu>  list_1 =  symenuService.listPrivateByUserId(param);
//            if(menu.getUrl()!=null&&menu.getUrl().trim().length()!=0){
//                menus.add(request.getContextPath()+menu.getUrl());
//            }
//            menu.setChild(list_1);
//            for(Symenu menu_1:list_1){
//                param.put("menulevel","3");
//                param.put("punid",menu_1.getUnid()+"");
//                if(menu_1.getUrl()!=null&&menu_1.getUrl().trim().length()!=0){
//                    menus.add(request.getContextPath()+menu_1.getUrl());
//                }
//                List<Symenu>  list_2 =  symenuService.listPrivateByUserId(param);
//                menu_1.setChild(list_2);
//            }
//        }
//        CommSession.setMenu(request,menus);
        return null;
    }
    @ResponseBody
    @RequestMapping("checkSessionStatus")
    public Map checkSessionStatus(HttpServletRequest request){
        Map result = new HashMap<>();

        if(MapUtil.get("outtime")!=null&&(boolean)MapUtil.get("outtime")){
            result.put("result",false);
        } else{
            result.put("result",true);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("session")
    public ModelAndView sessionDis(HttpServletRequest request){
        ModelAndView view = new ModelAndView("back/session");
        return view;

    }

    @ResponseBody
    @RequestMapping("notlogin")
    public ModelAndView notlogin(HttpServletRequest request){
        ModelAndView view = new ModelAndView("back/notlogin");
        return view;

    }
}